package cc.plumgo.ss.ui.page;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;

import cc.plumgo.j2me.utils.CustomPage;
import cc.plumgo.ss.core.Constants;

public class ShowPage extends CustomPage {
	public static final int SHOW_HIDE = 0;
	public static final int SHOW_SHOW = 1;
	public static final int SHOW_STATE_NUM = 2;

	protected String imgName;
	protected int delay = 5;
	protected Image backImage;
	protected int arrowX;
	protected int arrowY;
	
	public ShowPage(String imgName, int x, int y) {
		super();
		this.imgName = imgName;
		arrowX = x;
		arrowY = y;
	}

	public void process(int keyStates, Graphics g) {
		switch (state) {
		case SHOW_HIDE:
			break;
		case SHOW_SHOW: {
			drawBackImage(g);
			
			loadSprite(Constants.UI_IMG_ARROW, Constants.UI_ARROW_FRM_SIZ[0],
					Constants.UI_ARROW_FRM_SIZ[1]);
			drawArrow(g);
			state = (keyStates & GameCanvas.FIRE_PRESSED) != 0 ? SHOW_HIDE : state;
			delay = (-- delay < 0)? 5 : delay;
			break;
		}
		}
	}
	
	protected void drawBackImage(Graphics g) {
		if (backImage == null) {
			try {
				backImage = Image.createImage(imgName);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		int x = (scrWidth - backImage.getWidth()) / 2;
		int y = (scrHeight - backImage.getHeight()) / 2;
		g.drawImage(backImage, x, y, 0);
	}
	
	protected void drawArrow(Graphics g) {
		int frm = sprite.getFrame();
		if (delay == 0) {
			if (frm == 0) {
				sprite.setFrame(1);
			} else {
				sprite.setFrame(0);
			}
		}
		sprite.setPosition(arrowX, arrowY);
		sprite.paint(g);
	}
}
